﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;
using HML.Design;

namespace HML
{
    /// <summary>
    /// 全局按钮集合
    /// </summary>
    [Editor(typeof(CollectionHelpEditor), typeof(UITypeEditor))]
    public class TabControlPlusGlobalButttonCollection : IList, ICollection, IEnumerable
    {
        #region 字段

        /// <summary>
        /// 全局按钮集合
        /// </summary>
        private ArrayList arrayList = new ArrayList();

        /// <summary>
        /// 全局按钮所属TabControl
        /// </summary>
        private TabControlPlus owner;

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="owner">全局按钮所属TabControl</param>
        public TabControlPlusGlobalButttonCollection(TabControlPlus owner)
        {
            this.owner = owner;
        }

        #region IEnumerable

        public IEnumerator GetEnumerator()
        {
            TabControlPlusGlobalButtton[] listArray = new TabControlPlusGlobalButtton[this.arrayList.Count];
            for (int index = 0; index < listArray.Length; ++index)
            {
                listArray[index] = (TabControlPlusGlobalButtton)this.arrayList[index];
            }
            return listArray.GetEnumerator();
        }

        #endregion

        #region ICollection

        public void CopyTo(Array array, int index)
        {
            for (int i = 0; i < this.Count; i++)
            {
                array.SetValue(this.arrayList[i], i + index);
            }
        }

        public int Count
        {
            get { return this.arrayList.Count; }
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get { return (object)this; }
        }

        #endregion

        #region IList

        public int Add(object value)
        {
            if (!(value is TabControlPlusGlobalButtton))
            {
                throw new ArgumentException("TabControlPlusGlobalButtton");
            }
            return this.Add((TabControlPlusGlobalButtton)value);
        }

        public int Add(TabControlPlusGlobalButtton item)
        {
            item.SetOwner(this.owner);
            this.arrayList.Add(item);
            if (this.owner != null)
            {
                this.owner.InitializeElement();
                this.owner.Invalidate();
            }
            return this.Count - 1;
        }

        public void Clear()
        {
            for (int i = 0; i < this.arrayList.Count; i++)
            {
                ((TabControlPlusGlobalButtton)this.arrayList[i]).SetOwner(null);
            }
            this.arrayList.Clear();
            if (this.owner != null)
            {
                this.owner.InitializeElement();
                this.owner.Invalidate();
            }
        }

        public bool Contains(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            return this.IndexOf(value) != -1;
        }

        bool IList.Contains(object item)
        {
            if (item is TabControlPlusGlobalButtton)
            {
                return this.Contains((TabControlPlusGlobalButtton)item);
            }
            return false;
        }

        public int IndexOf(object item)
        {
            if (item is TabControlPlusGlobalButtton)
            {
                return this.arrayList.IndexOf(item);
            }
            return -1;
        }

        public void Insert(int index, object value)
        {
            if (index > this.Count)
            {
                index = this.Count;
            }
            ArrayList tmp = new ArrayList();
            for (int i = 0; i < index; i++)
            {
                tmp.Add(this.arrayList[i]);
            }
            ((TabControlPlusGlobalButtton)value).SetOwner(this.owner);
            tmp.Add(value);
            for (int i = index; i < this.Count; i++)
            {
                tmp.Add(this.arrayList[i]);
            }
            this.arrayList = tmp;
        }

        public bool IsFixedSize
        {
            get { return false; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public void Remove(object value)
        {
            if (!(value is TabControlPlusGlobalButtton))
            {
                throw new ArgumentException("TabControlPlusGlobalButtton");
            }
            this.Remove((TabControlPlusGlobalButtton)value);
        }

        public void Remove(TabControlPlusGlobalButtton item)
        {
            item.SetOwner(null);
            this.arrayList.Remove(item);
            if (this.owner != null)
            {
                this.owner.InitializeElement();
                this.owner.Invalidate();
            }
        }

        public void RemoveAt(int index)
        {
            ((TabControlPlusGlobalButtton)this.arrayList[index]).SetOwner(null);
            this.arrayList.RemoveAt(index);
            if (this.owner != null)
            {
                this.owner.InitializeElement();
                this.owner.Invalidate();
            }
        }

        public void RemoveByKey(string key)
        {
            int index = this.IndexOfKey(key);
            if (this.IsValidIndex(index))
            {
                this.RemoveAt(index);
            }
        }

        public TabControlPlusGlobalButtton this[int index]
        {
            get { return (TabControlPlusGlobalButtton)this.arrayList[index]; }
            set
            {
                ((TabControlPlusGlobalButtton)this.arrayList[index]).SetOwner(null);
                value.SetOwner(this.owner);
                this.arrayList[index] = value;
                if (this.owner != null)
                {
                    this.owner.InitializeElement();
                    this.owner.Invalidate();
                }
            }
        }

        object IList.this[int index]
        {
            get { return (object)this.arrayList[index]; }
            set
            {
                ((TabControlPlusGlobalButtton)this.arrayList[index]).SetOwner(null);
                ((TabControlPlusGlobalButtton)value).SetOwner(this.owner);
                this.arrayList[index] = value;
                if (this.owner != null)
                {
                    this.owner.InitializeElement();
                    this.owner.Invalidate();
                }
            }
        }

        public virtual TabControlPlusGlobalButtton this[string key]
        {
            get
            {
                if (string.IsNullOrEmpty(key))
                {
                    return null;
                }

                int index = this.IndexOfKey(key);
                if (this.IsValidIndex(index))
                {
                    return this[index];
                }
                else
                {
                    return null;
                }

            }
        }

        public virtual int IndexOfKey(String key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return -1;
            }

            for (int i = 0; i < this.Count; i++)
            {
                if (((TabControlPlusGlobalButtton)this.arrayList[i]).Key == key)
                {
                    return i;
                }
            }

            return -1;
        }

        private bool IsValidIndex(int index)
        {
            return ((index >= 0) && (index < this.Count));
        }

        #endregion

    }

}
